package com.example.manager.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.enums.PluginTypeEnum;
import com.example.manager.TrafficManager;
import com.example.mapper.TrafficMapper;
import com.example.model.TrafficDO;
import com.example.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author yinxin
 * @version 1.0
 * @Description:
 * @date 2022/6/2214:00
 */
@Component
@Slf4j
public class TrafficManagerImpl implements TrafficManager {

    @Autowired
    TrafficMapper trafficMapper;

    @Override
    public int add(TrafficDO trafficDO) {
        return trafficMapper.insert(trafficDO);
    }

    @Override
    public IPage<TrafficDO> pageAvailable(int page, int size, Long accountNo) {
        Page<TrafficDO> pageInfo = new Page<TrafficDO>(page, size);
        String today = TimeUtil.format(new Date(), "yyyy-MM-dd");
        return trafficMapper.selectPage(pageInfo, new QueryWrapper<TrafficDO>()
                .eq("account_no", accountNo)
                .ge("expired_date", today)
                .orderByDesc("gmt_create"));
    }

    @Override
    public TrafficDO findByIdAndAccountNo(Long trafficId, Long accountNo) {
        return trafficMapper.selectOne(new QueryWrapper<TrafficDO>()
                .eq("account_no",accountNo)
                .eq("id",trafficId));
    }

    /**
     * 给吗某个流量包增加天使用次数
     * @param currentTrafficId 当前流量纪律id
     * @param accountNo 账户id
     * @param dayUsedTimes 增加使用次数
     * @return
     */
//    @Override
//    public int addDayUsedTimes(Long currentTrafficId, Long accountNo, int dayUsedTimes) {
//        return trafficMapper.update(null, new UpdateWrapper<TrafficDO>()
//                .eq("account_no", accountNo)
//                .eq("id", currentTrafficId)
//                .set("day_used", dayUsedTimes));
//    }

    /**
     * 删除过期的流量包
     * @return
     */
    @Override
    public boolean deleteExpireTraffic() {
        int rows = trafficMapper.delete(new QueryWrapper<TrafficDO>().le("expired_date", new Date()));
        log.info("删除过期流量包{}条",rows);
        return true;
    }

    /**
     * 查找未过期的可用流量包列表(不一定可用，可能超过次数了)
     * @param accountNo
     * @return
     */
    @Override
    public List<TrafficDO> selectAvailableTraffics(Long accountNo) {
        String today = TimeUtil.format(new Date(), "yyyy-MM-dd");
        QueryWrapper<TrafficDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_no",accountNo);
        queryWrapper.and(wrapper->{
            wrapper.ge("expired_date", today)
                    .or()
                    .eq("out_trade_no","free_init");
        });
        queryWrapper.eq("plugin_type", PluginTypeEnum.SHORT_LINK.name());
        return trafficMapper.selectList(queryWrapper);
    }

    /**
     * 增加流量包使用次数
     * @param accountNo
     * @param trafficId
     * @param usedTimes
     * @return
     */
    @Override
    public int addDayUsedTimes(Long accountNo, Long trafficId, Integer usedTimes) {
        return trafficMapper.addDayUsedTimes(accountNo, trafficId, usedTimes);
    }

    /**
     * 减少流量包使用次数
     * @param accountNo
     * @param trafficId
     * @param usedTimes
     * @return
     */
    @Override
    public int releaseUsedTimes(Long accountNo, Long trafficId, Integer usedTimes,String useDateStr) {
        return trafficMapper.releaseUsedTimes(accountNo, trafficId, usedTimes,useDateStr);
    }

    /**
     * 批量更新流量包使用次数为0 用完第二天恢复
     * @param accountNo
     * @param unUpdatedTrafficIds
     */
    @Override
    public int batchUpdateUsedTimes(Long accountNo, List<Long> unUpdatedTrafficIds) {
        return trafficMapper.update(null, new UpdateWrapper<TrafficDO>()
                .eq("account_no", accountNo)
                .in("id", unUpdatedTrafficIds)
                .set("day_used", 0));
    }
}
